package server;

import java.net.Socket;
import java.util.Map;

public class RequestProcessor extends Thread {

  private final Socket socket;
  private final Request request;
  private final Response response;
  private final String url;
  private final Map<String, HttpServlet> servletMap;

  public RequestProcessor(Socket socket, Request request, Response response,
                          String url, Map<String, HttpServlet> servletMap) {
    this.socket = socket;
    this.request = request;
    this.response = response;
    this.url = url;
    this.servletMap = servletMap;
  }

  @Override
  public void run() {
    try {
      // 静态资源处理
      if (servletMap.get(url) == null) {
        response.outputHtml(url);
      } else {
        // 动态资源servlet请求
        HttpServlet httpServlet = servletMap.get(url);
        httpServlet.service(request, response);
      }

      socket.close();

    } catch (Exception e) {
      e.printStackTrace();
    }

  }
}
